home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 288_01.zip / ARTICLE.TSP < prev    next >
Text File  |  1993-04-01  |  14KB  |  219 lines

  1. A Poor Man's Solution to the Traveling Salesman Problem
  2.  
  3. Kevin E Knauss                                                       12 Mar 89
  4.  
  5. Given a map and a means of transportation, a competent traveling salesman can
  6. pick a reasonable route to all his customers.  The route he picks needn't be 
  7. optimal just practical.  In this article, we'll explore a hypothetical 
  8. salesman's intuitive approach to finding a practical route to all his 
  9. customers and its implementation in the C programming language.  In an attempt
  10. to find elegant optimal solutions to tough problems, we often overlook
  11. solutions which appear to be rather "brutish."  Upon closer inspection,
  12. however, we see that these solutions are more elegant than they appear and,
  13. better yet, they work!
  14.  
  15. BACKGROUND
  16.  
  17. Routing and scheduling problems are inherently difficult to solve because they
  18. often require total enumeration of all possible outcomes.  As the number of
  19. data points in the problem increases, the possible outcomes increase
  20. exponentially.  With the Traveling Salesman Problem (TSP) for instance,
  21. studies have shown that an algorithm which yields an exact solution is
  22. relatively infeasible for networks containing in excess of 100 points.  In
  23. fact, there are problems with as few as 48 cities for which the best answer
  24. found has not been proven to be optimal.  By using heuristics of various
  25. types, one is enabled to find feasible (though not necessarily optimal)
  26. solutions to these otherwise "unsolvable" problems.
  27.  
  28. The TSP simply stated is: "A salesman, starting in one city, wishes to visit 
  29. each n─1 other cities once and only once and return to the start.  In what
  30. order should he visit the cities to minimize the total distance traveled?" (8) 
  31. This may seem too trivial a task to have generated active research for over
  32. three decades (the literature I've researched goes back to 1958), but there 
  33. are many practical applications for the solution of this basic problem.
  34. Automated drilling machines and the newer laser drills used to drill printed
  35. circuit boards, for example, may have hundreds or thousands of holes to drill
  36. and often spend as much time traveling to a position for a hole as they do
  37. drilling it.  Programming efficient head travel for these machines could make
  38. the difference for a company to turn a profit or loss.  Solve the more basic
  39. TSP, and the marginal printed circuit board company may be able to stay in
  40. business by applying the same techniques.
  41.  
  42. The TSP is one which is, to quote an old cliche, "easier said than done."
  43. That is, it is easy to explain the problem but difficult to solve; at least it
  44. seems difficult to solve when we look at many of the purely mathematical
  45. models that are, too often, not related back to the original problem.  I chose
  46. to attack the TSP in a simplistic manner in an attempt to find one or more
  47. algorithms which would approximate a person's intuitive approach.  In so 
  48. doing, I was able to find an efficient way to "solve" the problem by producing
  49. acceptable results to large─scale traveling salesman problems.  (Note that the
  50. word "acceptable" implies that judgments are required.)
  51.  
  52. TERMINOLOGY
  53.  
  54. Before we begin traveling around, a review of TSP terminology is in order.
  55. We'll begin with the city, our most basic term.  This is what will be visited 
  56. and may also be referred to as a town, point, node, or vertex.  One goes from
  57. one city to the next by traveling a given distance or incurring a specified
  58. cost.  The terms link, arc, and edge are also used in place of distance or
  59. cost.  The collection of all the cities and the distances between each pair is
  60. a network or graph and is often represented by a distance matrix.  A salesman
  61. will follow a route to visit each of the cities in the network, and this route
  62. may also be called a tour, path, or circuit.  Finally, if we remove two or
  63. more links from the completed tour, we will break it into sub─paths or chains
  64. of cities.
  65.  
  66. The distance matrix is a two dimensional array where the horizontal or row
  67. vector (dimension) is identical to the vertical or column vector.  The cell
  68. found at each intersection contains the distance or cost between the city
  69. represented by the horizontal coordinate and the city represented by the
  70. vertical coordinate.  Those familiar with graph theory haven't seen anything 
  71. new here.  If you've seen a lot of these terms for the first time, however, 
  72. don't be afraid to refer back, for I'll be using many of them interchangeably.
  73.  
  74. APPROACH
  75.  
  76. Let's now consider the problem in terms of a salesman who must visit a dozen 
  77. or so cities in the state of Hypothetica.  Since the salesman must leave and
  78. return to the same city and visit all other cities in the process, his tour
  79. will be some sort of loop through the state.  Obviously, as a loop is
  80. unbroken, one may start at any point on the the tour and still trace the same
  81. loop.  Thus the starting city is of no consequence; rather we want to find the
  82. best route irregardless of the salesman's starting point.
  83.  
  84. Intuitively, one would want to travel to cities nearby and to cities near
  85. those.  We can build a procedure based on this thought by first finding the
  86. closest two cities and then continuing to the next closest city that hasn't 
  87. been visited.  This should produce a fairly good tour, or at least would seem
  88. so at first.  It may turn out that this tour isn't optimal, but it's a
  89. reasonable solution for starters.
  90.  
  91. As the cities are exhausted from our network, we have fewer choices to make.
  92. Intuitively, we may reason that the choices left to us may not be as good as
  93. those we're offered in the early stages of tour building.  Our salesman may be 
  94. forced to backtrack and cross previously traversed arcs.  If we check the
  95. proximity of neighboring cities, however, especially those near the end of the
  96. initial tour, we may be able to find improvements.
  97.  
  98. One approach we may try involves the removal of a single city from the tour
  99. and testing it between each pair of cities in the remaining tour.  Once we've 
  100. tested it in each location, we'll place it in the location where the overall 
  101. circuit cost is lowest (i.e. the shortest distance the salesman must travel).
  102. This same approach may be tried with chains of cities of varying lengths, but
  103. with chains we must also check for orientation (that is try the chain both
  104. frontward and backward between each pair of cities).  This leaves us with our
  105. last thought of simply checking the orientation of a chain in its original
  106. location.  If you think a picture is worth a thousand words, see Figure 1 so
  107. we can cut 4,000 words from this article.  By testing the proximity of every
  108. city or the proximity and orientation of every chain, we can be fairly
  109. confident that any ill effects produced by our original technique will be
  110. cleaned up.
  111.  
  112. If we look through related literature, we find that our tour building and
  113. improvement techniques have already been studied and named.  Our tour building
  114. algorithm is known as the nearest neighbor or greedy algorithm.  Our tour
  115. improvement algorithms generally fall into a category known as k─optimality or
  116. k─opt for short.  A tour is said to be k─optimal if we are unable to improve
  117. it by removing any k arcs and replacing them with k others.  Checking chain
  118. orientation in place is the same as removing two arcs and replacing them with
  119. two others and is thus the 2─opt algorithm.  Likewise, chain proximity and
  120. orientation is the 3─opt algorithm with point proximity a special case where
  121. the chain to be tested has length one.  Even though these improvement
  122. techniques are related, we'll evaluate each on its own merits.
  123.  
  124. IMPLEMENTATION
  125.  
  126. To evaluate the intuitive approach we may embark upon an elaborate
  127. mathematical analysis that may or may not produce any conclusive results, or
  128. we may implement the solutions in practical models that may be run against
  129. live data.  If this was a scientific journal, we'd follow the mathematical 
  130. tack; but since this is a practical journal, we'll try the modeling approach.
  131.  
  132. The main functions are programmed in their own modules called:  NearNeighbor,
  133. PointOpt, TwoOpt, Hybrid, and ThreeOpt (listings 1 through 5 respectively).
  134. NearNeighbor generates the initial tour from the distance matrix while the
  135. other routines take turns improving it.  PointOpt performs point proximity
  136. improvement only, and TwoOpt performs only chain orientation improvement.
  137. Hybrid combines point proximity and chain orientation improvements while
  138. ThreeOpt adds chain proximity and orientation.  The nearest neighbor, 2─Opt,
  139. and 3─Opt algorithms have been studied in detail within the field, but are
  140. normally regarded as independent techniques.  To my knowledge, this is the
  141. first that point proximity has been considered either independently (PointOpt)
  142. or in conjunction with the 2─Opt algorithm (Hybrid).
  143.  
  144. We'll use six distance matrices found in the literature to test our procedures 
  145. since these networks have known optima (or at least a best known solution as
  146. is the case of the 48 city problem).  We'll need to know the tour length each 
  147. procedure produces and the time it takes to find the tour.  We can calculate
  148. from this information how much improvement is made by each technique and what
  149. percentage each solution is from the known optimum.  To see how the
  150. improvement techniques work on different initial tours, we'll reverse the 
  151. initial nearest neighbor tour and generate a bad initial tour.
  152.  
  153. To capture the time, we'll need a system dependent routine.  GetTime samples 
  154. the clock counter by issuing an interrupt under MS─DOS; ElapsedTime calls
  155. GetTime and compares the new time with a previous time passed in.  Listing 6
  156. shows GetTime implemented using the MIX C compiler for MS─DOS and ElapsedTime
  157. in a plain vanilla implementation.  For the bad initial tour, we'll simply 
  158. reverse the logic of the nearest neighbor algorithm to generate a farthest
  159. neighbor tour (listing 7).  The driver program (listing 8) is far from
  160. elegant but gets the job done.
  161.  
  162. OBSERVATIONS
  163.  
  164. One might assume that, since it embodies all the techniques used in the other
  165. improvement algorithms, the 3─Opt algorithm would produce a tour at least as
  166. good as the others.  Our results show that one might be wrong in such an
  167. assumption!  In fact, the 3─Opt algorithm only found the best solution in the
  168. two smallest problems and other algorithms found the same solutions (see
  169. Figure 2).  The total time to run the three tour building routines and the
  170. three lesser improvement routines on each is less than the time needed to run
  171. the 3─Opt routine just once on one of the larger problems (see Figure 3). This
  172. indicates that the 3─Opt algorithm may not be very valuable as a tour
  173. improvement algorithm.
  174.  
  175. The independent point proximity algorithm is likewise not very valuable.  It
  176. was 15% faster than the Hybrid routine overall but fell well behind in
  177. performance.  PointOpt found the best solution in the 10 city problem, but
  178. Hybrid found the same solution.  Hybrid outperformed PointOpt in all the other
  179. solutions so nothing is gained by running both routines.
  180.  
  181. We can see that the TwoOpt and Hybrid routines compliment each other very
  182. nicely.  Where Hybrid didn't find the best solution, TwoOpt did.  The 2─Opt 
  183. algorithm was consistently the fastest and the point proximity 2─Opt hybrid
  184. found the best answer in four of the six problems.  Their combined times plus
  185. the time to build the initial tour was comparable to the time required to read
  186. the distance matrix on my 12Mhz AT clone with 28ms access hard drive.
  187.  
  188. One thing that may be a surprise, is that our tour improvement algorithms are
  189. dependent upon how compatible the initial tour is with the techniques being
  190. applied and not necessarily how close to optimum that tour is.  In all but one
  191. problem, the best improvement was found when starting from the nearest
  192. neighbor solution.  In the 42 city problem, however, the best improvement was
  193. found by starting with the farthest neighbor solution.  In addition to being
  194. found from the nearest neighbor solution, the same improvement was found in
  195. less time from the farthest neighbor solution in the 10 and 20 city problems.
  196.  
  197. The MIX C compiler/linker with its .COM executable files causes some problems
  198. for large scale applications such as the drilling machines.  I had no problem
  199. with our small to medium scale problems, but when I compiled the program with
  200. dimensions over 100 the program ran out of space.  Dynamic storage allocation
  201. won't cure the problem since the heap space for dynamic storage allocation and 
  202. the stack space for static data structures all come from the same pot. Answers
  203. will have to come from a C environment that allows for .EXE executable files
  204. or the use of disk resident dynamic arrays.  The latter of these will degrade
  205. execution time, but taking a long time to find a solution is better than
  206. taking a short time to not find one at all!  Note that by using the function
  207. ArcCost to access distance matrix data we've made it easy to try differing 
  208. storage methods.
  209.  
  210. CONCLUSION
  211.  
  212. Following an intuitive approach to a problem and implementing that approach
  213. can often produce very acceptable results.  Though there can be no substitute
  214. for thorough analysis, neither can there be a substitute for experimentation
  215. and testing of hypotheses.  The modularity of C with its procedures and
  216. functions allows the building blocks of experimentation to become the building
  217. blocks of application.  With our TSP modeling, we need only develop a new
  218. driver program to build an efficient production package.
  219.